package com.zuipin.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.product.ProductArea;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IProductAreaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @ClassName ProductAreaController
 * @author wangcongyi
 * @date 2017年5月8日 下午6:20:44
 */
@Controller
@RequestMapping(value = "/productArea")
@Api(value = "商品可售区域", description = "商品可售区域-wangcongyi")
public class ProductAreaController {
	
	@Resource
	private IProductAreaService productAreaService;
	
	@ResponseBody
	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ApiOperation(value = "测试控制器 通过id查询可销区域", notes = "测试控制器 通过id查询可销区域")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "商品可销区域ID", required = true, dataType = "long", paramType = "query") })
	public ProductArea findById(@RequestParam("id") Long id) {
		
		return productAreaService.selectByPrimaryKey(id);
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateProArea", method = RequestMethod.POST)
	@ApiOperation(value = "二期-新增或更新商品可售区域(后台)", notes = "新增或更新商品可售区域(后台)")
	@Valids({ @Valid(name = "productArea.id"), @Valid(name = "productArea.proExtId", required = true), @Valid(name = "productArea.provinceId", required = true),
			@Valid(name = "productArea.cityId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "productArea.id", value = "ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productArea.proExtId", value = "商品拓展信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productArea.provinceId", value = "省份ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "productArea.cityId", value = "城市ID", required = true, dataType = "long", paramType = "query") })
	public Result<?> saveOrUpdateProArea(@Inject @ApiIgnore ProductArea productArea) {
		Result<Boolean> result = new Result<>();
		boolean flag = false;
		if (null != productArea.getId()) {
			flag = productAreaService.updateByPrimaryKeySelective(productArea);
			if (flag) {
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("商品可售区域修改成功！");
			} else {
				result.setErrorCode(ErrorCode.ELSE);
				result.setMessage("商品可售区域修改失败！");
			}
		} else {
			flag = productAreaService.insertSelective(productArea);
			if (flag) {
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setMessage("商品可售区域添加成功！");
			} else {
				result.setErrorCode(ErrorCode.ELSE);
				result.setMessage("商品可售区域添加失败！");
			}
		}
		return result;
	}
}
